import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:grocery_app_demo/api/entity/wait_for_purchase_goods_entity.dart';
import 'package:grocery_app_demo/api/scm_api.dart';
import 'package:grocery_app_demo/widgets/gap.dart';

class WaitForPurchaseOrdersLogic extends GetxController {
  RxList<WaitForPurchaseGoodsEntity> waitForPurchaseGoods = RxList();

  @override
  void onReady() {
    ScmApi()
        .getList<WaitForPurchaseGoodsEntity>(ScmApi.waitForPurchaseGoods)
        .then((value) => waitForPurchaseGoods.addAll(value));
  }

  Widget getGoodsSelectIcon(WaitForPurchaseGoodsEntity g) {
    return IconButton(
      onPressed: () => toggleGoodsSelect(g),
      icon: isSelectNoneOrder(g)
          ? const Icon(Icons.check_box_outline_blank)
          : isSelectAllOrder(g)
              ? const Icon(Icons.check_box)
              : const Icon(Icons.indeterminate_check_box),
    );
  }

  void toggleGoodsSelect(WaitForPurchaseGoodsEntity g) {
    if (isSelectAllOrder(g)) {
      goodsOrderSelectedSet
          .removeWhere((e) => e.startsWith("${goodsShipperKey(g)}-"));
    } else {
      goodsOrderSelectedSet
          .addAll(g.orders!.map((e) => goodsShipperOrderKey(g, e.orderId!)));
    }
  }

  bool isSelectAllOrder(WaitForPurchaseGoodsEntity g) {
    return goodsOrderSelectedSet
            .where((e) => e.startsWith("${goodsShipperKey(g)}-"))
            .length ==
        g.orders!.length;
  }

  bool isSelectNoneOrder(WaitForPurchaseGoodsEntity g) {
    return goodsOrderSelectedSet
        .where((e) => e.startsWith("${goodsShipperKey(g)}-"))
        .isEmpty;
  }

  RxSet<String> expandedGoodsCardSet = RxSet();
  RxSet<String> goodsOrderSelectedSet = RxSet();

  bool isGoodsOrderSelected(WaitForPurchaseGoodsEntity g, int orderId) {
    return goodsOrderSelectedSet.contains(goodsShipperOrderKey(g, orderId));
  }

  String goodsShipperOrderKey(WaitForPurchaseGoodsEntity g, int orderId) =>
      "${goodsShipperKey(g)}-$orderId";

  String goodsShipperKey(WaitForPurchaseGoodsEntity g) {
    return "${g.goodsCode}-${g.shipperId}";
  }

  bool isGoodsCardExpand(WaitForPurchaseGoodsEntity g) {
    return expandedGoodsCardSet.contains(goodsShipperKey(g));
  }

  void toggleGoodsCardExpand(WaitForPurchaseGoodsEntity g) {
    if (isGoodsCardExpand(g)) {
      expandedGoodsCardSet.remove(goodsShipperKey(g));
    } else {
      expandedGoodsCardSet.add(goodsShipperKey(g));
    }
  }

  List<Widget> buildGoodsCards() {
    return waitForPurchaseGoods
        .map((g) => Card(
              key: ValueKey(goodsShipperKey(g)),
              margin: gapAll16,
              child: Column(
                children: [
                  Row(
                    children: [
                      getGoodsSelectIcon(g),
                      buildCardHeader(g),
                    ],
                  ),
                  isGoodsCardExpand(g) ? buildOrderList(g) : shrinkBox
                ],
              ),
            ))
        .toList();
  }

  Widget buildOrderList(WaitForPurchaseGoodsEntity g) {
    List<Widget> list = [];
    for (var o in g.orders!) {
      list.add(line);
      list.add(InkWell(
        onTap: () => toggleOrderSelect(g, o),
        child: ListTile(
          leading: isGoodsOrderSelected(g, o.orderId!)
              ? const Icon(Icons.check_box)
              : const Icon(Icons.check_box_outline_blank),
          title: Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text("收货人：${o.consignee}"),
                    Text("数量：${o.count}"),
                    shrinkBox,
                  ],
                ),
                Text("地址简称：${o.address}"),
                Text("配送时间：${o.stationTime}"),
              ],
            ),
          ),
        ),
      ));
    }
    return Column(
      children: list,
    );
  }

  void toggleOrderSelect(
      WaitForPurchaseGoodsEntity g, WaitForPurchaseGoodsOrders o) {
    if (isGoodsOrderSelected(g, o.orderId!)) {
      goodsOrderSelectedSet.remove(goodsShipperOrderKey(g, o.orderId!));
    } else {
      goodsOrderSelectedSet.add(goodsShipperOrderKey(g, o.orderId!));
    }
  }

  Widget buildCardHeader(WaitForPurchaseGoodsEntity g) {
    return Expanded(
      child: InkWell(
        onTap: () => toggleGoodsCardExpand(g),
        child: Padding(
          padding: gapAll16,
          child: Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    g.goodsName!,
                    style: const TextStyle(
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Text("货主：${g.shipperName}"),
                  shrinkBox,
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("需求数量：${g.count}"),
                  Text("重量(公斤)：${g.weight}"),
                  isGoodsCardExpand(g)
                      ? const Icon(Icons.keyboard_double_arrow_up)
                      : const Icon(Icons.keyboard_double_arrow_down),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
